# encoding = utf-8
from application.utils.MySQLUtils import MySQLUtils
import pandas as pd
from application.logging import logger


def connect_mysql_read_weather_data(area_code):
    """
    查询天气数据：根据地区编码
    :param area_code: 地区编码
    :return:
    """
    # 查询语句
    sql = f"SELECT area_code,uptime,humidity,winp,weather,temp_curr " \
          f"FROM public_basic_forecast_weather WHERE source_code=2 AND area_code='{area_code}' " \
          f"GROUP BY uptime HAVING COUNT(id)>=1"
    # 打印语句
    logger.info(sql)
    # 获取连接
    con = MySQLUtils.get_mysql_connection()
    # 执行查询
    df = pd.read_sql_query(sql, con=con)
    # 筛选数据
    df = df[["area_code", "uptime", "humidity", "winp", "weather", "temp_curr"]]
    # 排重数据
    df = df.drop_duplicates()
    # 打印数据
    logger.info(f"天气数据:{'*' * 50}")
    logger.info(df)
    logger.info(f"天气数据:{'*' * 50}")
    # 返回数据
    return df
    pass


if __name__ == '__main__':
    # 测试数据：110106000000
    connect_mysql_read_weather_data(110106000000)
    pass
